package svm;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;

import utils.Logger;
import utils.Utils;
import au.AU;

public abstract class SvmContext implements Serializable {

	private static final long serialVersionUID = 1L;

	protected List<Session> sessionList;
	protected List<AU> aus;
	protected File parentDir;

	public SvmContext(String parentDirPath, List<AU> aus) {
		this.parentDir = new File(parentDirPath);
		this.aus = aus;
		Logger.log("SvmContext", "created context for parent directory: " + parentDirPath);
	}
	
	public void createLuxandData() throws Exception {
		LuxandDataUtils.createLuxandData(parentDir.getAbsolutePath());
	}
	
	public List<Session> getSessions() {
		return sessionList;
	}
	
	public void save(String fileName) {
        try {
        	File f = new File(this.parentDir.getAbsolutePath() + "/" + fileName);
        	if(!f.exists()) f.createNewFile();
        	Utils.clearFile(f);
	        FileOutputStream fileOut = new FileOutputStream(f);
	        ObjectOutputStream out = new ObjectOutputStream(fileOut);
			out.writeObject(this);
	        out.close();
	        fileOut.close();
	        Logger.log("SvmContext", this.getClass().toString().replace("class svm.", "") + " context saved to " + f.getAbsolutePath());
		} catch (IOException e) {
			Logger.log(this.getClass().toString().replace("class svm.", ""), "ERROR: cannot save context");
			e.printStackTrace();
		}
	}
	
	public static SvmContext load(String filePath) {
		SvmContext c = null;
	    try {
	    	FileInputStream fileIn = new FileInputStream(filePath);
	       	ObjectInputStream in = new ObjectInputStream(fileIn);
	       	c = (SvmContext) in.readObject();
	       	in.close();
	       	fileIn.close();
		    Logger.log("SvmContext", " created context from file " + filePath);
	    } catch(IOException i) {
	    	Logger.log("SvmContext", "ERROR: file " + filePath + " not found");
	    } catch(ClassNotFoundException e) {
	    	Logger.log("SvmContext", "ERROR: class not found");
	    }
	    
       	return c;
	}

	//public abstract void loadLuxandData();
	public abstract void loadLuxandData(HashMap<String, String> keywords);

}
